package de.openkanban.client.ui.presenter;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.util.SC;

import de.openkanban.client.ui.board.KanbanEditView;
import de.openkanban.shared.dto.UserDto;

public class GetKanbanUserRequestCallback implements RequestCallback {

	KanbanEditView kanbanEditView;

	public GetKanbanUserRequestCallback(KanbanEditView kanbanEditView) {
		super();
		this.kanbanEditView = kanbanEditView;
	}

	@Override
	public void onResponseReceived(Request request, Response response) {
		if (response.getStatusCode() < 300) {
			JSONArray kanbanUser = new JSONArray(JSON.decode(response.getText()));
			List<UserDto> userResultList = new ArrayList<UserDto>();

			for (int i = 0; i < kanbanUser.size(); i++) {
				UserDto user = new UserDto(kanbanUser.get(i).isObject().toString());
				userResultList.add(user);
			}

			kanbanEditView.showKanbanUser(userResultList);
		} else {
			SC.say("Fehler: " + response.getStatusText());
		}

	}

	@Override
	public void onError(Request request, Throwable exception) {
	}

}
